package service

import (
	"common_demo/internal/config"
	"common_demo/pkg/logger"
	rds "common_demo/pkg/store/redis"
	"common_demo/pkg/store/sqlite"
	"context"

	"github.com/redis/go-redis/v9"
)

type ServiceGroup struct {
	SqliteCli sqlite.Operator
	RedisCli  *redis.Client
}

func NewServiceGroup(ctx context.Context) *ServiceGroup {
	redisCli, err := rds.NewRedisClient(ctx, config.Config.Redis)
	if err != nil {
		logger.Errorf("failed to connect to redis, error: %s", err.Error())
	}

	sqliteDB, err := sqlite.NewSqliteDB(config.Config.Database.Path)
	if err != nil {
		logger.Errorf("failed to connect to database, error: %s", err.Error())
	}

	return &ServiceGroup{
		RedisCli:  redisCli,
		SqliteCli: sqliteDB,
	}
}

func (service *ServiceGroup) AddThing(thing any) error {
	return service.SqliteCli.Table("tbl_things").Insert(map[string]any{
		"key": thing,
	})
}
